/**   
* @Package com.app.open.sys.menu.service 
*/
package com.wlyy.bcwlw.sys.menu.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.wlyy.bcwlw.common.cache.CacheEvict;
import com.wlyy.bcwlw.common.tree.BaseStaticTreeNode;
import com.wlyy.bcwlw.common.tree.StaticTree;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.common.tree.ZTreeDTO;
import com.wlyy.bcwlw.sys.SecurityService;
import com.wlyy.bcwlw.sys.application.dao.ApplicationDAO;
import com.wlyy.bcwlw.sys.application.entity.ApplicationDTO;
import com.wlyy.bcwlw.sys.menu.dao.MenuDAO;
import com.wlyy.bcwlw.sys.menu.dao.ModuleDAO;
import com.wlyy.bcwlw.sys.menu.entity.MenuDTO;
import com.wlyy.bcwlw.sys.menu.entity.ModuleDTO;
import com.wlyy.bcwlw.sys.permission.dao.PermissionMenuDAO;
import com.wlyy.bcwlw.sys.permission.entity.PermissionMenuDTO;

/** 
 * <p>Description: TODO</p>
 * @author zhangyang
 * <p>日期:2015年6月27日 下午5:20:26</p> 
 * @version V1.0 
 */
@Transactional
@Service
public class MenuService {
	@Autowired
	private MenuDAO menuDAO;
	@Autowired
	private ApplicationDAO applicationDAO;
	@Autowired
	private ModuleDAO moduleDAO;
	@Autowired
	private SecurityService securityService;
	@Autowired
	private PermissionMenuDAO permissionMenuDAO;
	
	
	private final String MENU_ISDISPLAY="1";
	private final String MENU_ISNOTDISPLAY = "0";
	
	private final String VirtualBaseTree ="VirtualBaseTree";
	
	@CacheEvict(key="userMenusCache")
	public boolean createNewMenu(MenuDTO menuDTO){
		String id =menuDAO.create(menuDTO);
		if(id!=null){
			return true;
		}
		return false;
	}
	
	
	public TreeNode getMenuTree(String id,Boolean display){
		List<BaseStaticTreeNode> nodeList = new ArrayList<BaseStaticTreeNode>();
		ModuleDTO moduleDTO = new ModuleDTO();
		MenuDTO menu = new MenuDTO();
		if (display != null) {
			if(display){
				menu.setDisplay(MENU_ISDISPLAY);
			}
		}
		//有问题
		if(StringUtils.isNotEmpty(id)){
			moduleDTO.setAppId(id);
			nodeList.add(new BaseStaticTreeNode(id, "菜单", null, "app"));
		}else{
			nodeList.add(new BaseStaticTreeNode(VirtualBaseTree, "系统菜单", null, "base"));
			List<ApplicationDTO> appList = applicationDAO.queryByObject(null);
			if(appList !=null && appList.size() !=0){
				for(ApplicationDTO app:appList){
					nodeList.add(new BaseStaticTreeNode(app.getAppId(), app.getAppName(), VirtualBaseTree, "app"));
				}
			}
		}
		List<ModuleDTO> sortModuleList = moduleDAO.queryListByCriteria(moduleDTO, "sort asc", null, null);
		for(ModuleDTO dto:sortModuleList){
			BaseStaticTreeNode treeNode = new BaseStaticTreeNode(dto.getId(), dto.getName(), dto.getAppId(), "module");
			nodeList.add(treeNode);
		}
		List<MenuDTO> sortMenuList = menuDAO.queryListByCriteria(menu, "sort asc", null, null);
		
		
		for (MenuDTO dto : sortMenuList) {
			BaseStaticTreeNode treeNode = new BaseStaticTreeNode(dto.getId(), dto.getName(), dto.getModuleId(), "menu","",dto.getUrl());
			if (StringUtils.isNotEmpty(dto.getDisplay()) && MENU_ISNOTDISPLAY.equals(dto.getDisplay())) {
				treeNode.attr("hide", "true");
			}
			nodeList.add(treeNode);
		}
		StaticTree tree = new StaticTree(nodeList.toArray(new BaseStaticTreeNode[0]),id);
		tree.render();
		return tree.getRootNode();
		
	}
	
	
	public MenuDTO queryWithModuleNameById(String id){
		return menuDAO.queryWithModuleNameById(id);
	}
	
	@CacheEvict(key={"userMenusCache","userMenuUrlsCache"})
	public MenuDTO save(MenuDTO menuDTO){
		if(StringUtils.isEmpty(menuDTO.getId())){
			menuDAO.create(menuDTO);
		}else{
			MenuDTO oldMenuDTO = menuDAO.findById(menuDTO.getId());
			if(oldMenuDTO == null){
				return null;
			}
			menuDAO.update(menuDTO);
		}
		//刷新系统受保护路径
		securityService.refreshProtectedUrls();
		return menuDTO;
	}
	
	public String getAllMenu(boolean display){
		List<MenuDTO> menuList = menuDAO.queryListByCriteria(null, "sort asc", null, null);
		
		List<ZTreeDTO> treeList = new ArrayList<ZTreeDTO>();
		
		treeList.add(new ZTreeDTO(VirtualBaseTree,"0","系统菜单"));
		
	
//		if(menuList != null && menuList.size()!=0){
//			for(MenuDTO menu:menuList){
//				ZTreeDTO tree = new ZTreeDTO(menu.getId(),menu.getParentId(),menu.getName());
//				treeList.add(tree);
//			}
//		}
		return JSON.toJSONString(treeList);
		
		
	}
	
	@CacheEvict(key={"userMenusCache","userMenuUrlsCache"})
	public Boolean deleteMenuById(String menuId){
		PermissionMenuDTO dto = new PermissionMenuDTO();
		dto.setMenuId(menuId);
		permissionMenuDAO.remove(dto);
		return menuDAO.removeById(menuId);
	}

}
